package com.hourse.platform.controller;

import com.hourse.platform.entity.*;
import com.hourse.platform.service.RoleService;
import com.hourse.platform.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "系统-角色服务")
@RestController
@RequestMapping("platform/role")
@Slf4j
public class RoleController {

    @Autowired
    private RoleService roleService;

    /**
     * @功能描述: 【创建或修改角色】
     */
    @ApiOperation(value = "系统-创建或修改角色", response = RoleVO.class)
    @SneakyThrows
    @PostMapping(value = "saveRole")
    public PageData saveRole(@RequestBody RoleVO roleVO) {

        RoleEntity roleEntity =
                (RoleEntity) roleVO.bindEnity(RoleEntity.class);

        roleService.saveOrUpdate(roleEntity);

        return roleVO.SUCCESS();
    }

    /**
     * @功能描述: 【获取菜单列表】
     */
    @ApiOperation(value = "系统-角色列表", response = RoleVO.class)
    @SneakyThrows
    @GetMapping(value = "roleList")
    public ResultMsg<List<RoleVO>> roleList() {

        return new ResultMsg(roleService.getRoleList());
    }

    /**
     * @功能描述: 【创建或修改角色菜单】
     */
    @ApiOperation(value = "系统-创建或修改角色菜单", response = RoleMenuVO.class)
    @SneakyThrows
    @PostMapping(value = "saveRoleMenu")
    public PageData saveRoleMenu(@RequestBody RoleMenuVO roleMenuVO) {

        RoleMenuEntity roleMenuEntity =
                (RoleMenuEntity) roleMenuVO.bindEnity(RoleMenuEntity.class);

        roleService.saveOrUpdate(roleMenuEntity);
        roleMenuVO.setId(roleMenuEntity.getId());

        return roleMenuVO.SUCCESS();
    }

    /**
     * @功能描述: 【创建或修改角色菜单】
     */
    @ApiOperation(value = "系统-删除角色菜单", response = RoleMenuVO.class)
    @SneakyThrows
    @PostMapping(value = "deleteRoleMenu")
    public PageData deleteRoleMenu(@RequestBody RoleMenuVO roleMenuVO) {

        roleService.deleteRoleMenu(roleMenuVO);

        return roleMenuVO.SUCCESS();
    }


    /**
     * @功能描述: 【创建或修改角色菜单】
     */
    @ApiOperation(value = "系统-删除角色权限按钮", response = RoleMenuButtonVO.class)
    @SneakyThrows
    @PostMapping(value = "deleteRoleMenuButton")
    public PageData deleteRoleMenuButton(@RequestBody RoleMenuButtonVO roleMenuButtonVO) {

        roleService.deleteRoleMenuButton(roleMenuButtonVO);

        return roleMenuButtonVO.SUCCESS();
    }

    /**
     * @功能描述: 【获取角色菜单】
     */
    @ApiOperation(value = "系统-角色菜单权限列表", response = RoleMenuVO.class)
    @GetMapping(value = "roleMenuList/{roleId}")
    public ResultMsg<List<RoleMenuVO>> menuButtonList(@PathVariable String roleId) {

        return new ResultMsg(roleService.getRoleMenuList(roleId));
    }

    /**
     * @功能描述: 【创建或修改角色菜单】
     */
    @ApiOperation(value = "系统-创建角色菜单按钮权限", response = RoleMenuButtonVO.class)
    @SneakyThrows
    @PostMapping(value = "saveRoleMenuButton")
    public PageData saveRoleMenuButton(@RequestBody RoleMenuButtonVO roleMenuButtonVO) {

        RoleMenuButtonEntity roleMenuButtonEntity =
                (RoleMenuButtonEntity) roleMenuButtonVO.bindEnity(RoleMenuButtonEntity.class);

        roleService.saveOrUpdate(roleMenuButtonEntity);
        roleMenuButtonVO.setId(roleMenuButtonEntity.getId());

        return roleMenuButtonVO.SUCCESS();
    }

    /**
     * @功能描述: 【获取角色菜单】
     */
    @ApiOperation(value = "系统-创建角色菜单按钮权限列表", response = RoleMenuButtonVO.class)
    @GetMapping(value = "roleMenuButtonList/{roleId}/{menuId}")
    public ResultMsg roleMenuButtonList(
            @PathVariable String roleId,
            @PathVariable String menuId) {


        return new ResultMsg(roleService.getRoleMenuButtonList(roleId, menuId));
    }


    /**
     * @return : com.hourse.platform.vo.Message
     * @功能描述: 【获取菜单列表】
     */
    @ApiOperation(value = "系统-菜单列表")
    @SneakyThrows
    @GetMapping(value = "menuList")
    public ResultMsg<List<MenuVO>> menuList(@RequestParam(required = false) String menuId
            , @RequestParam String roleId) {

        return new ResultMsg(roleService.getMenuList(menuId, roleId));
    }


    /**
     * @功能描述: 【获取菜单按钮】
     */
    @ApiOperation(value = "系统-菜单按钮列表")
    @SneakyThrows
    @GetMapping(value = "menuButtonList/{menuId}/{roleId}")
    public ResultMsg<List<MenuButtonEntity>> menuButtonList(@PathVariable String menuId, @PathVariable String roleId) {

        return new ResultMsg(roleService.getMenuButtonList(menuId, roleId));
    }


    /**
     * @功能描述: 【创建或修改角色】
     */
    @ApiOperation(value = "系统-添加修改角色组", response = RoleVO.class)
    @SneakyThrows
    @PostMapping(value = "saveRoleGroup")
    public PageData saveRoleGroup(@RequestBody RoleVO roleVO) {

        RoleGroupEntity roleGroupEntity =
                (RoleGroupEntity) roleVO.bindEnity(RoleGroupEntity.class);

        roleService.saveOrUpdate(roleGroupEntity);

        return roleVO.SUCCESS();
    }

    /**
     * @功能描述: 【获取菜单列表】
     */
    @ApiOperation(value = "系统-角色组列表", response = RoleVO.class)
    @SneakyThrows
    @GetMapping(value = "roleGroupList")
    public ResultMsg<List<RoleVO>> roleGroupList() {

        return new ResultMsg(roleService.getRoleGroupList());
    }

    /**
     * @功能描述: 【获取菜单列表】
     */
    @ApiOperation(value = "系统-角色组ITEM列表", response = RoleVO.class)
    @SneakyThrows
    @GetMapping(value = "roleGroupItemList/{roleGroupId}")
    public ResultMsg<List<RoleGroupItemVO>> roleGroupItemList(@PathVariable String roleGroupId) {

        return new ResultMsg(roleService.roleGroupItemList(roleGroupId));
    }

    /**
     * @功能描述: 【角色组添加角色】
     */
    @SneakyThrows
    @PostMapping(value = "saveRoleGroupItem")
    public PageData saveRoleGroupItem(@RequestBody RoleGroupItemVO roleGroupItemVO) {

            RoleGroupItemEntity roleGroupItemEntity =
                    (RoleGroupItemEntity) roleGroupItemVO.bindEnity(RoleGroupItemEntity.class);

            roleService.saveOrUpdate(roleGroupItemEntity);

            return roleGroupItemVO.SUCCESS();
    }

    /**
     * @功能描述: 【角色组添加角色】
     */
    @SneakyThrows
    @GetMapping(value = "delteRoleGroupItem/{id}")
    public PageData delteRoleGroupItem(@PathVariable String id) {

        roleService.delete(id, RoleGroupItemEntity.class);

        return PageData.New().SUCCESS();
    }
}
